<?php
declare (strict_types = 1);

namespace app\common\model\mysql;

/**
 * @mixin \think\Model
 */
class Goods extends BaseModel
{
    /**
     * 图片路径生成器
     *
     * @param $value
     */
    public function getImageAttr($value)
    {
        return request()->domain().$value;
    }

    /**
     * 轮播图路径生成器
     *
     * @param $value
     * @return mixed|string[]
     */
    public function getCarouselImageAttr($value)
    {
        if (!empty($value)) {
            $image = explode(',', $value);
            $value = array_map(function($val){
                return request()->domain().$val;
            }, $image);
        }
        return $value;
    }

    /**
     * 标题搜索器
     *
     * @param $query
     * @param $value
     * @param $data
     */
    public function searchTitleAttr($query, $value)
    {
        $query->where('title', 'like', "%$value%");
    }

    /**
     * 添加事件搜索器
     *
     * @param $query
     * @param $value
     * @param $data
     */
    public function searchCreateTimeAttr($query, $value)
    {
        $query->whereBetweenTime('create_time', $value[0], $value[1]);
    }

    /**
     * 获取当前分页数据
     *
     * @param array $data
     * @param int $number
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getLists(array $searchKey, array $data, int $number = 10)
    {
        if ($searchKey) {
            $res = $this->withSearch($searchKey, $data);
        } else {
            $res = $this;
        }
        $where = [['status', 'in', [config('status.mysql.table_waiting'), config('status.mysql.table_normal')]]];
        $order = ['listorder' => 'desc', 'id' => 'desc'];
        $list  = $res->where($where)->order($order)->paginate($number);
        return $list;
    }

    /**
     * 通过条件获取商品数据
     *
     * @param array $where
     * @param int $limit
     * @return Goods[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNormalGoodsByCondition(array $where, $field = true, int $limit = 5)
    {
        $where['status'] = config('status.mysql.table_normal');
        $order = ['listorder' => 'desc', 'id'        => 'desc'];
        $res   = $this->field($field)->where($where)->order($order)->limit($limit)->select();
        return $res;
    }

    /**
     * 根据对应栏目 ID 获取对应商品
     *
     * @param int $categoryId
     * @param bool $field
     * @param int $limit
     * @return Goods[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNormalGoodsFindInSetCategoryId(int $categoryId, $field = true, int $limit = 10)
    {
        $order = ['listorder' => 'desc', 'id' => 'desc'];
        $where = ['status' => config('status.mysql.table_normal')];

        $result = $this->whereFindInSet('category_path_id', $categoryId)
                        ->where($where)
                        ->field($field)
                        ->order($order)
                        ->limit($limit)
                        ->select();
        return $result;
    }

    /**
     * 获取列表页对应分类数据
     *
     * @param array $data
     * @param int $number
     * @param bool $field
     * @param array $order
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getNormalLists(array $data = [], int $number = 5, $field = true, array $order = ['listorder' => 'desc', 'id' => 'desc'])
    {
        if (isset($data['category_path_id'])) {
            $res = $this->whereFindInSet('category_path_id', $data['category_path_id']);
        } else {
            $res = $this;
        }
        $lists = $res->where('status', config('status.mysql.table_normal'))
                    ->field($field)
                    ->order($order)
                    ->paginate($number);
        return $lists;
    }
}
